package com.blockchain.console.servlets.util;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.blockchain.common.enums.CoinType;
import com.blockchain.console.service.financial.entity.NeoBalanceEntity;
import com.blockchain.console.service.financial.entity.NeoResultEntity;
import com.blockchain.console.service.financial.entity.NeoTradeResultEntity;
import com.blockchain.console.service.financial.entity.NeoTransactionEntity;
import com.blockchain.framework.service.exception.ParameterException;
import com.blockchain.util.HttpRequest;

public class NEOUtil implements Serializable {

	private static final long serialVersionUID = 1L;
	
	private static Object[] getNEOParams(Object...params){
		if (params == null) {
			params = new Object[]{};
		}
		return params;
	}
	
	public static Map<String, Object> getNewAccount(String ip , String port, String pass) throws Throwable {
		StringBuffer url = new StringBuffer(ip).append(":" + port);
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("jsonrpc","2.0");
		map.put("method","getnewaddress");
		map.put("id","1");
		map.put("params", getNEOParams(pass));
		String result = HttpRequest.sendPost( url.toString() , JsonUtil.GsonString(map),CoinType.NEO.getName());
		Map<String, Object> json = JsonUtil.GsonToMaps(result);
		return json;
	}

	public static BigDecimal getBalance(String ip , String port,String contractAddress) throws Throwable {
		StringBuffer url = new StringBuffer(ip).append(":" + port);
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("jsonrpc","2.0");
		map.put("method","getbalance");
		map.put("id","1");
		map.put("params", getNEOParams(contractAddress));
		String result = HttpRequest.sendPost( url.toString() , JsonUtil.GsonString(map),CoinType.NEO.getName());
		NeoResultEntity json = JsonUtil.GsonToBean(result, NeoResultEntity.class);
		if (json.error != null) {
			throw new ParameterException("代币钱包链接失败" + json.error);
		}else{
			NeoBalanceEntity balanceEntity = JsonUtil.GsonToBean(json.result.toString(), NeoBalanceEntity.class);
			return new BigDecimal(balanceEntity.balance);	
		}
	}

	public static NeoTransactionEntity 
	sendTransaction(String ip , String port, String assetId, String toaddress, String num) throws Throwable {
		StringBuffer url = new StringBuffer(ip).append(":" + port);
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("jsonrpc","2.0");
		map.put("method","sendtoaddress");
		map.put("id","1");
		map.put("params", getNEOParams(assetId, toaddress, num));
	
		String result = HttpRequest.sendPost( url.toString() , JsonUtil.GsonString(map),CoinType.NEO.getName());
		NeoResultEntity json = JsonUtil.GsonToBean(result, NeoResultEntity.class);
		NeoTransactionEntity tradeJson = JsonUtil.GsonToBean(json.result.toString(), NeoTransactionEntity.class);
		return tradeJson;
	}
	
	public static NeoResultEntity getBlockCount(String ip, String port) throws Throwable {
		StringBuffer url = new StringBuffer(ip).append(":" + port);
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("jsonrpc","2.0");
		map.put("method","getblockcount");
		map.put("id","1");
		map.put("params", getNEOParams());
	
		String result = HttpRequest.sendPost( url.toString() , JsonUtil.GsonString(map),CoinType.NEO.getName());
		NeoResultEntity json = JsonUtil.GsonToBean(result, NeoResultEntity.class);
		
		return json;
	}


	
	public static List<NeoTransactionEntity> getTransactionByBlockNumber(String ip, String port, int block) throws Throwable {
		StringBuffer url = new StringBuffer(ip).append(":" + port);
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("jsonrpc","2.0");
		map.put("method","getblock");
		map.put("id","1");
		map.put("params", getNEOParams(block, "1"));
	
		String result = HttpRequest.sendPost( url.toString() , JsonUtil.GsonString(map),CoinType.NEO.getName());
		NeoResultEntity json = JsonUtil.GsonToBean(result, NeoResultEntity.class);
		NeoTradeResultEntity tradeJson = JsonUtil.GsonToBean(json.result.toString(), NeoTradeResultEntity.class);
		return tradeJson.tx;
	}

	
	public static NeoTransactionEntity getTransactionByHash(String ip, String port, String hash) throws Throwable {
		StringBuffer url = new StringBuffer(ip).append(":" + port);
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("jsonrpc","2.0");
		map.put("method","getrawtransaction");
		map.put("id","1");
		map.put("params", getNEOParams(hash, "1"));
	
		String result = HttpRequest.sendPost( url.toString() , JsonUtil.GsonString(map),CoinType.NEO.getName());
		NeoResultEntity json = JsonUtil.GsonToBean(result, NeoResultEntity.class);
		NeoTransactionEntity tradeJson = JsonUtil.GsonToBean(json.result.toString(), NeoTransactionEntity.class);
		return tradeJson;
	}

	public static String getTxid(Object result) {
		
		return JsonUtil.GsonToMaps(result.toString()).get("txid").toString();
	}
	
}
